package com.question.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.util.AjaxResponse;
import com.common.util.JpaPageable;
import com.common.util.TablePage;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.question.entity.Single;
import com.question.entity.dto.BlankDTO;
import com.question.entity.dto.SingleDTO;
import com.question.service.ISingleService;
import com.user.entity.dto.TeacherDTO;
import com.user.service.ITeacherService;

@Controller
@RequestMapping("/single")
public class SingleController {
	@Autowired
	private ISingleService singleService;
	@Autowired
	private ITeacherService teacherService;
	
	@RequestMapping("/findAll")
	public @ResponseBody List<SingleDTO> findAll(){
		try {
			return singleService.findAll();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/findAll2")
	public @ResponseBody List<Single> findAll2(){
		return singleService.findAll2();
	}
	@RequestMapping("/saveOrUpdate")
	public @ResponseBody AjaxResponse saveOrUpdate(String single) {
		try {
			ObjectMapper objectMapper = new ObjectMapper();
			Single obj = objectMapper.readValue(single,Single.class);
			if(singleService.isNumExist(obj))
				return new AjaxResponse(false, "编号已存在!");
			if(obj.getId()!=null&&singleService.isUsing(obj.getId()))
				return new AjaxResponse(false, "题目已被使用，不允许修改!");
			singleService.saveOrUpdate(obj);
			return new AjaxResponse(true, "操作成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return new AjaxResponse(false, "系统出错！");
		}
		
	}
	@RequestMapping("/findPage")
	public @ResponseBody TablePage<SingleDTO> findPage(JpaPageable pageable,HttpSession session,String str,Long subject_id){
		try {
			List<Long> ids = new ArrayList<Long>();
			
			TeacherDTO dto = (TeacherDTO) session.getAttribute("teacher");
			if(null!=dto)
				ids = teacherService.getSubjectsId(dto.getId());
			
			return singleService.findPage(SingleDTO.getSpecification(ids,str,subject_id), pageable.getPageable());

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/delete")
	public @ResponseBody AjaxResponse delete(Long id){
		try {
			if(singleService.isUsing(id))
				return new AjaxResponse(false, "题目已被使用，不允许删除!");
			singleService.delete(id);
			return new AjaxResponse(true, "已删除!");
		} catch (Exception e) {
			e.printStackTrace();
			return new AjaxResponse(false, "删除失败!");
		}
	}
	@RequestMapping("/findRealPage")
	public @ResponseBody TablePage<Single> findRealPage(JpaPageable pageable,HttpSession session,String str,Long subject_id){
		try {
			List<Long> ids = new ArrayList<Long>();
			
			TeacherDTO dto = (TeacherDTO) session.getAttribute("teacher");
			if(null!=dto)
				ids = teacherService.getSubjectsId(dto.getId());
			
			return singleService.findRealPage(SingleDTO.getSpecification(ids,str,subject_id), pageable.getPageable());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/findOne")
	public @ResponseBody Single findOne(Long id) {
		try {
			return singleService.findOne(id);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/testSave")
	public void testSave() {
		singleService.testSave();
	}
}
